package com.wsoft.hr.pm.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.core.utils.LoginUserUtil;
import com.wsoft.hr.pm.entity.HrPmProcessTemplateCardEntity;
import com.wsoft.hr.pm.entity.HrPmProcessTemplateEntity;
import com.wsoft.hr.pm.mapper.HrPmProcessTemplateMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author zhonglj
 * @Date 2025/3/1 16:56
 */
@Log4j2
@Service
public class HrPmProcessTemplateManager extends ServiceImpl<HrPmProcessTemplateMapper, HrPmProcessTemplateEntity> {
    @Resource
    private HrPmProcessTemplateCardManager processTemplateCardManager;

    /**
     * 考评模版考核流程复制
     *
     * @param fromTemplateId
     * @param toTemplateId
     * @return void
     * @author zhonglj
     * @date 2025/3/12 13:19
     **/
    @Transactional(rollbackFor = Exception.class)
    public void copyProcessTemplate(Long fromTemplateId, Long toTemplateId) {
        List<HrPmProcessTemplateEntity> fromTemplateEntities = list(
                Wrappers.lambdaQuery(HrPmProcessTemplateEntity.class)
                        .eq(HrPmProcessTemplateEntity::getTemplateId, fromTemplateId)
                        .orderByAsc(HrPmProcessTemplateEntity::getSort)
        );

        if (CollUtil.isNotEmpty(fromTemplateEntities)) {
            LocalDateTime now = DateUtil.toLocalDateTime(new Date());
            Long userId = LoginUserUtil.getUserInfo().getLoginUserVO().getId();

            fromTemplateEntities.stream().forEach(template -> {
                Long oldPid = template.getId();
                template.setId(null);
                template.setTemplateId(toTemplateId);
                template.setCreateTime(now);
                template.setCreateBy(userId.toString());
                template.setUpdateTime(null);
                template.setUpdateBy(null);
                save(template);
                Long newPid = template.getId();

                // 卡片数据
                List<HrPmProcessTemplateCardEntity> fromCardEntities = processTemplateCardManager.list(
                        Wrappers.lambdaQuery(HrPmProcessTemplateCardEntity.class)
                                .eq(HrPmProcessTemplateCardEntity::getPid, oldPid)
                                .orderByAsc(HrPmProcessTemplateCardEntity::getSerialSort)
                                .orderByAsc(HrPmProcessTemplateCardEntity::getParallelSort)
                );

                if (CollUtil.isNotEmpty(fromCardEntities)) {
                    List<HrPmProcessTemplateCardEntity> cardEntities = fromCardEntities.stream().map(card -> {
                        card.setId(null);
                        card.setPid(newPid);
                        card.setCreateTime(now);
                        card.setCreateBy(userId.toString());
                        card.setUpdateTime(null);
                        card.setUpdateBy(null);

                        return card;
                    }).collect(Collectors.toList());

                    processTemplateCardManager.saveBatch(cardEntities);
                }
            });

        }
    }
}
